/*
 * http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx
 */

package android.bluebox.model;

import java.util.ArrayList;

import android.bluebox.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AValueAdapter extends BaseAdapter {

	private static ArrayList<AValue> aValueList;

	private LayoutInflater mInflater;

	public AValueAdapter(Context context, ArrayList<AValue> list) {
		aValueList = list;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return aValueList.size();
	}

	@Override
	public Object getItem(int position) {
		return aValueList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.avalue_row_view, null);
			holder = new ViewHolder();

			holder.txtName = (TextView) convertView.findViewById(R.id.avl_name);
			holder.txtTagList = (TextView) convertView.findViewById(R.id.avl_tag);
			//			holder.txtWorkspaceList = (TextView) convertView.findViewById(R.id.avl_ws);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.txtName.setText(aValueList.get(position).getName());

		String strTagList = "";

		for (int id : aValueList.get(position).getTagIdList()) {
			Tag tag = StaticBox.dbBox.getTag(id);
			if (tag != null) {
				strTagList += tag.getName();
				strTagList += "; ";
			}
		}

		if (strTagList.length() > 2)
			strTagList = strTagList.substring(0, strTagList.length() - 2);
		holder.txtTagList.setText(strTagList);

		return convertView;
	}

	static class ViewHolder {
		TextView txtName;
		TextView txtTagList;
	}

	public void setArraylist(ArrayList<AValue> list) {
		aValueList = list;
	}
}
